package com.sgb.kjwl.view.ui.activity.self.userInfo;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityForgetBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.view.widget.SimpleCountDownTimer;
import com.sgb.kjwl.viewmodel.self.ForgetModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.MToast;

import javax.inject.Inject;

public class ForgetActivity extends AppActivity {
    private ActivityForgetBinding binding;
    private String title;
    @Inject
    ForgetModel viewModel;

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    public static void start(Context context, String title) {
        Intent intent = new Intent(context, ForgetActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(ForgetActivity.this, R.layout.activity_forget);
        binding.setViewModel(viewModel);
        title = getIntent().getStringExtra("title");
        binding.include.titleView.setText(title);
        binding.include.ivBack.setVisibility(View.VISIBLE);
        binding.include.ivBack.setOnClickListener(this);
        binding.ivCode.setOnClickListener(this);
        binding.ivLogin.setOnClickListener(this);
    }

    @Override
    protected void init() {

    }


    @Override
    public void onMultiClick(View v) {
        super.onMultiClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_login:
                loginValidation();
                break;
            case R.id.iv_code:
                codeValidation();
                break;
        }
    }

    private void loginValidation() {
        if (TextUtils.isEmpty(viewModel.account.get())) {
            MToast.showToast(ForgetActivity.this, "手机号码不能为空");
            return;
        }
        if (TextUtils.isEmpty(viewModel.code.get())) {
            MToast.showToast(ForgetActivity.this, "短信验证码不能为空");
            return;
        }

        if (!CheckUtils.isMobileNum(viewModel.account.get())) {
            MToast.showToast(ForgetActivity.this, "请输入正确手机格式");
            return;
        }
        if (viewModel.code.get().length() != 6) {
            MToast.showToast(ForgetActivity.this, "验证码长度为6位数");
            return;
        }
        if (TextUtils.isEmpty(viewModel.pwd.get())) {
            MToast.showToast(ForgetActivity.this, "新密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(viewModel.pwdEnsure.get())) {
            MToast.showToast(ForgetActivity.this, "确认密码不能为空");
            return;
        }
        if (!CheckUtils.isPassword(viewModel.pwd.get())) {
            MToast.showToast(ForgetActivity.this, R.string.reg_pwd);
            return;
        }
        if (!viewModel.pwdEnsure.get().equals(viewModel.pwd.get())) {
            MToast.showToast(ForgetActivity.this, "两次输入密码不一致");
            return;
        }

        viewModel.forgetPwdModel();
    }

    private void codeValidation() {
        if (TextUtils.isEmpty(viewModel.account.get())) {
            MToast.showToast(ForgetActivity.this, "手机号码不能为空");
            return;
        }
        if (!CheckUtils.isMobileNum(viewModel.account.get())) {
            MToast.showToast(ForgetActivity.this, "请输入正确手机格式");
            return;
        }
        SimpleCountDownTimer mCountDownTimerUtils = new SimpleCountDownTimer(this,binding.ivCode, 60000, 1000); //倒计时1分钟
        mCountDownTimerUtils.start();;
        viewModel.getCodeModel();
    }
}
